package cn.com.headfree.dictionary.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.dictionary.dao.IDictionaryDao;
import cn.com.headfree.dictionary.entity.TCommonDictionary;

@Repository("dictionaryDao")
public class DictionaryDaoImpl extends BaseDaoImpl<TCommonDictionary> implements IDictionaryDao {

    @Override
    public void findPageDictionaryByPId(String pId, String sysCode, String name, Page<TCommonDictionary> page) {
        List<String> params = new ArrayList<String>(3);
        StringBuffer sql = new StringBuffer("select t1.* from t_common_dictionary t1 where t1.p_id=? and t1.del_sign=0 ");
        if ("root".equals(pId)) {
            sql = new StringBuffer("select t1.* from t_common_dictionary t1,t_common_dictionary t2 where t2.p_id=? and t1.p_id=t2.id and t1.del_sign=0 ");
        }
        params.add(pId);
        if (!StringUtil.isNullOrEmpty(sysCode)) {
            params.add(sysCode);
            sql.append(" and t1.sys_code=? ");
        }
        if (!StringUtil.isNullOrEmpty(name)) {
            params.add("%" + name + "%");
            sql.append(" and t1.name like ? ");
        }
        this.findAsPageBySql(TCommonDictionary.class, sql.toString(), page, null, true, params.toArray());
    }

    @Override
    public List<TCommonDictionary> getAllByParentValue(String parentValue) {
        String sql = "select t.id,t.`name`,t.`value`,t.p_id,t.remark,t.del_sign,t.create_time,t.update_time,t.updater_name,t.sys_code from t_common_dictionary t where 1=1 AND t.del_sign=0 AND t.p_id=(select id from t_common_dictionary g where g.value=?) order by UPDATE_TIME desc";
        return this.findAllBySQL(TCommonDictionary.class, sql, null, false, parentValue);
    }

}
